using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using Q.Framework.Extension;
using Q.Framework.Utility;

namespace Q.Framework.WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:DateControl runat=server></{0}:DateControl>")]
    public class DateControl : CompositeControl, IBindableControl
    {
        public string DataPropertyName { get; set; }

        private String controlBindableProperty = "Value";
        public string ControlBindableProperty
        {
            get { return controlBindableProperty; }
            set { controlBindableProperty = value; }
        }

        private QTextBox txtData = new QTextBox() { ValueType = TypeExpression.Data };
        private ImageButton imbCalendar = new ImageButton();
        private CalendarExtender calendarButtonExtender = new CalendarExtender();

        private DateTime minDate = DateTime.MinValue;
        public DateTime MinDate
        {
            get { return minDate; }
            set { minDate = value; }
        }

        private DateTime maxDate = DateTime.MaxValue;
        public DateTime MaxDate
        {
            get { return maxDate; }
            set { maxDate = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime Value
        {
            get { EnsureChildControls(); return txtData.ToDateTime(); }
            set { EnsureChildControls(); txtData.Text = value.Format(FormatDateTime.Date); }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime? NullableValue
        {
            get { EnsureChildControls(); return txtData.AsNullable<DateTime>(); }
            set { EnsureChildControls(); txtData.Text = value.HasValue ? value.Value.Format(FormatDateTime.Date) : String.Empty; }
        }

        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set { txtData.Enabled = value; imbCalendar.Enabled = value; }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            txtData.RenderControl(writer);
            imbCalendar.RenderControl(writer);
            writer.Write("<br />");
            calendarButtonExtender.RenderControl(writer);
        }

        protected override void CreateChildControls()
        {
            txtData.ID = this.ID + "Data";
            txtData.Width = new Unit(100, UnitType.Pixel);
            txtData.ValueType = TypeExpression.Data;

            imbCalendar.ID = this.ID + "Calendar";
            imbCalendar.ImageUrl = "~/imagens/DateControl.png";
            imbCalendar.AlternateText = "Clique para mostrar o calendário";
            imbCalendar.ImageAlign = ImageAlign.Middle;

            calendarButtonExtender.BehaviorID = this.ID + "calendarButtonExtender";
            calendarButtonExtender.PopupButtonID = imbCalendar.ID;
            calendarButtonExtender.TargetControlID = txtData.ID;
            calendarButtonExtender.StartDate = MinDate;
            calendarButtonExtender.EndDate = MaxDate;
            this.Controls.Add(txtData);
            this.Controls.Add(imbCalendar);
            this.Controls.Add(calendarButtonExtender);
            base.CreateChildControls();
        }

        private Boolean required;

        public Boolean Required
        {
            get { return required; }
            set { EnsureChildControls(); required = value; txtData.Required = value; }
        }

        public override void Focus()
        {
            EnsureChildControls();
            txtData.Focus();
        }
    }
}